package com.zshy.base.mq.config;

import com.zshy.base.mq.client.MQClient;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;

import java.util.List;

/**
 * 基础MQ配置
 * @author zhouhengzhe
 */
@Order(Ordered.HIGHEST_PRECEDENCE + 10)
@Configuration
@ConditionalOnProperty(prefix = "zshy.base.mq", name = "enable", havingValue = "true")
public class BaseMQConfig {

    @Autowired
    private List<MQClient> mqClients;

    @PostConstruct
    public void init() {
        if (mqClients != null && !mqClients.isEmpty()) {
            // 获取所有实现了MQClient的MQ客户端实现类
            for (MQClient mqClient : mqClients) {
                // 先初始化
                mqClient.init();
                // 再启动
                mqClient.start();
            }
        }
    }
}